//
//  MqRegisterViewController.m
//  Meiqi
//
//  Created by TwistedFate on 16/5/20.
//  Copyright © 2016年 KFC. All rights reserved.
//

#import "MqRegisterViewController.h"

@interface MqRegisterViewController ()
@property (weak, nonatomic) IBOutlet UITextField *account;
@property (weak, nonatomic) IBOutlet UITextField *pwd;
@property (weak, nonatomic) IBOutlet UITextField *pwdAgain;


@end

@implementation MqRegisterViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//  返回
- (IBAction)getBack:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}


//  注册
- (IBAction)registerAccount:(id)sender {

    
    NSString *registString = [NSString stringWithFormat:@"http://localhost/regist.php?account=%@&password=%@",_account.text, _pwd.text];
    
    if ([_pwd.text isEqualToString:_pwdAgain.text]) {
        
        [PKRequestManager requestWithUrlString:registString parDic:nil Method:GET finish:^(NSData *data) {
            
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil];
            
//            NSLog(@"%@",dic[@"state"]);
            
            NSString *msg = @"";
            if ([dic[@"state"] isEqualToString:@"true"]){
                
                msg = @"注册成功";
                
            }else{
                
                msg = @"该账号已经注册过";
            }
            
            //  提示框
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
            
            [alert show];
            
            //  提示两秒后关闭
            [self performSelector:@selector(removeAlert:) withObject:alert afterDelay:1];
            
        } error:^(NSError *error) {
            
            
        } path:nil ];

    }else{
        
        //  提示框
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"两次密码输入不一致" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        
        [alert show];
        
        //  关闭提示框
        [self performSelector:@selector(removeAlert:) withObject:alert afterDelay:1];
    }
    
}



 
//  延迟两秒关闭提示框
- (void)removeAlert:(UIAlertView *)alert{
    
    [alert dismissWithClickedButtonIndex:0 animated:YES];
    
    if ([alert.message isEqualToString:@"注册成功"]){
        
        //  返回登陆界面
        [self dismissViewControllerAnimated:YES completion:nil];
        
    }
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
